#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

 struct ListNode {
      int val;
      struct ListNode *next;
  };
  struct ListNode *detectCycle(struct ListNode *head) 
{
    struct ListNode *linkhead=head;
    if(head==NULL||head->next==NULL)
    return NULL;
    struct ListNode *fast=head;
    struct ListNode *slow=head;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
        if(slow==fast)
            break;
    }
    if (fast == NULL || fast->next == NULL)
        return NULL;
    while(linkhead)
    {
        if(linkhead==fast)
            break;
        linkhead=linkhead->next;
        fast=fast->next;
    }
return linkhead;
}